home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 17 / CU Amiga Magazine's Super CD-ROM 17 (1997)(EMAP Images)(GB)[!][issue 1997-12].iso / CUCD / Programming / DiceSource / src / derror / Save / Memory < prev   
Encoding:
Text File  |  1994-01-01  |  1.7 KB  |  104 lines

  1. /*
  2.  *  MALLOC.C
  3.  *
  4.  *  (c)Copyright 1990, Matthew Dillon, All Rights Reserved
  5.  */
  6.  
  7. #include <exec/memory.h>
  8. #include <errno.h>
  9.  
  10. extern long *__MemList;
  11.  
  12. void *
  13. malloc(size_t bytes, line)
  14. {
  15.     long *ptr;
  16.     char *p;
  17.  
  18.     if (bytes == 0)
  19.     return(NULL);
  20.  
  21.     ptr = AllocMem(bytes + 20, MEMF_PUBLIC);
  22.     if (ptr)
  23.     {
  24.     ptr[0] = (long)__MemList;
  25.     __MemList = ptr;
  26.     ptr[1] = bytes + 20;
  27.     ptr[2] = line;
  28.     ptr[3] = 0xabcdefab;
  29.     ptr += 4;
  30.     p = (char *)ptr+bytes;
  31.     p[0] = 0x57;
  32.     p[1] = 0x33;
  33.     p[2] = 0xAA;
  34.     p[3] = 0xE5;
  35.     }
  36.     else
  37.     {
  38.     errno = ENOMEM;
  39.     }
  40. if (run_arexx_server)
  41. {
  42.    run_arexx_server = 0;
  43.    printf("alloc %d bytes to %08lx from line %d\n", bytes, ptr, line);
  44.    run_arexx_server = 1;
  45. }
  46.     return((void *)ptr);
  47. }
  48.  
  49.  
  50. void
  51. free(void *vptr)
  52. {
  53.     long **scan = &__MemList;
  54.     long *item;
  55.     unsigned char *p;
  56.     long *ptr;
  57.  
  58.     if (vptr == NULL)
  59.     return;
  60.  
  61.     ptr = (long *)vptr - 4;
  62.  
  63.     while (item = *scan)
  64.     {
  65.     if (item == ptr)
  66.     {
  67.         *scan = *(long **)item;
  68.         //
  69.         // No make sure that the memory has not been trashed
  70.         //
  71.         if (ptr[3] != 0xabcdefab)
  72.         {
  73.            printf("Header trashed: Allocation %08lx from line %d\n", ptr+4, ptr[2]);
  74.         }
  75.         p = (char *)vptr + ptr[1] - 20;
  76.         if (p[0] != 0x57 ||
  77.             p[1] != 0x33 ||
  78.             p[2] != 0xaa ||
  79.             p[3] != 0xe5)
  80.         {
  81.            printf("Trailer trashed: Allocation %08lx from line %d\n", ptr+4, ptr[2]);
  82.         }
  83.  
  84. if (run_arexx_server)
  85. {
  86.    run_arexx_server = 0;
  87.    printf("Free %d bytes to %08lx from line %d\n", ptr[1]-20, ptr+4, ptr[2]);
  88.       run_arexx_server = 1;
  89. }
  90.         FreeMem(ptr, ptr[1]);
  91.         return;
  92.     }
  93.     scan = (long **)item;
  94.     }
  95. if (run_arexx_server)
  96. {
  97.    run_arexx_server = 0;
  98. printf("Free %08lx not on list from line %d\n", ptr+4, ptr[2]);
  99.    run_arexx_server = 1;
  100. }
  101.  
  102. }
  103.  
  104.